package com.example.neusoft.yuejian.Activity;


import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.neusoft.yuejian.Base.BaseActivity;
import com.example.neusoft.yuejian.Fragment.ClubFragment;
import com.example.neusoft.yuejian.Fragment.Exercisefragment;
import com.example.neusoft.yuejian.Fragment.MyFragment;
import com.example.neusoft.yuejian.Fragment.ShaituFragment;
import com.example.neusoft.yuejian.Fragment.ShouyeFragment;
import com.example.neusoft.yuejian.R;

public class ExerciseActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout ly_exercise, ly_club, ly_index, ly_friend, ly_my;
    private ImageView img_exercise, img_club, img_index, img_friend, img_my;
    private TextView tx_exercise, tx_club, tx_index, tx_friend, tx_my;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private Exercisefragment exercise;
    private ShouyeFragment shouye;
    private ShaituFragment shaitu;
    private ClubFragment club;
    private MyFragment my;


    @Override
    public int getLayoutFile() {
        return R.layout.activity_exe;
    }

    @Override
    public void initView() {
        ly_exercise = findViewById(R.id.ly_exercise);
        ly_club = findViewById(R.id.ly_club);
        ly_index = findViewById(R.id.ly_index);
        ly_my = findViewById(R.id.ly_my);
        ly_friend = findViewById(R.id.ly_friend);
        img_exercise = findViewById(R.id.img_exercise);
        img_club = findViewById(R.id.img_club);
        img_index = findViewById(R.id.img_index);
        img_friend = findViewById(R.id.img_friend);
        img_my = findViewById(R.id.img_my);
        tx_exercise = findViewById(R.id.tx_exercise);
        tx_club = findViewById(R.id.tx_club);
        tx_index = findViewById(R.id.tx_index);
        tx_friend = findViewById(R.id.tx_friend);
        tx_my = findViewById(R.id.tx_my);

    }

    @Override
    public void initEvent() {
        ly_exercise.setOnClickListener(this);
        ly_club.setOnClickListener(this);
        ly_index.setOnClickListener(this);
        ly_friend.setOnClickListener(this);
        ly_my.setOnClickListener(this);
    }

    @Override
    public void initData() {
            select(0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ly_exercise:
                select(0);
                break;
            case R.id.ly_index:
                select(1);
                break;
            case R.id.ly_friend:
                select(2);
                break;
            case R.id.ly_club:
                select(3);
                break;
            case R.id.ly_my:
                select(4);
                break;
        }
    }

    private void backimg() {
        img_exercise.setImageResource(R.drawable.ic_exercise);
        img_club.setImageResource(R.drawable.ic_club);
        img_index.setImageResource(R.drawable.ic_build);
        img_friend.setImageResource(R.drawable.ic_friend);
        img_my.setImageResource(R.drawable.ic_personal);
    }

    private void select(int i) {
        backimg();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hidefragment(fragmentTransaction);
        switch (i) {
            case 0:
                img_exercise.setImageResource(R.drawable.ic_exercise1);

                if (exercise == null) {
                    exercise = new Exercisefragment();
                    fragmentTransaction.add(R.id.framelayout, exercise);
                }
                else {
                    fragmentTransaction.show(exercise);
                }
                    break;

            case 1:
                img_index.setImageResource(R.drawable.ic_build1);
                if (shouye==null) {
                    shouye = new ShouyeFragment();
                    fragmentTransaction.add(R.id.framelayout, shouye);
                }
                else {
                    fragmentTransaction.show(shouye);
                }
                    break;
            case 2:
                img_friend.setImageResource(R.drawable.ic_friend1);
                if (shaitu==null) {
                    shaitu = new ShaituFragment();
                    fragmentTransaction.add(R.id.framelayout, shaitu);
                }
                else {
                    fragmentTransaction.show(shaitu);
                }
                    break;
            case 3:
                img_club.setImageResource(R.drawable.ic_club1);
                if (club==null) {
                    club = new ClubFragment();
                    fragmentTransaction.add(R.id.framelayout, club);
                }
                else {
                    fragmentTransaction.show(club);
                }
                    break;

            case 4:
                img_my.setImageResource(R.drawable.ic_personal1);
                if (my==null) {
                    my = new MyFragment();
                    fragmentTransaction.add(R.id.framelayout, my);
                }
                else {
                    fragmentTransaction.show(my);
                }
                   break;

        }
        fragmentTransaction.commit();
    }

    private void hidefragment(FragmentTransaction fragmentTransaction){
        if(exercise!=null){
            fragmentTransaction.hide(exercise);
        }
        if (shouye!=null){
            fragmentTransaction.hide(shouye);
        }
        if (shaitu!=null){
            fragmentTransaction.hide(shaitu);
        }
        if (club!=null){
            fragmentTransaction.hide(club);
        }
        if (my!=null){
            fragmentTransaction.hide(my);

    }
}}


